/**
 * Copyright (c) 2010-2011 Pierre LABATUT
 *
 * This file is part of multiple-track-player project.
 *
 * multiple-track-player is free software: you can redistribute it and/or modify it
 * under the terms of its license which may be found in the accompanying
 * LICENSE file or <http://code.google.com/p/multiple-track-player/>.
 */
package multitrack.preload;

/**
 *
 * @author Pierre LABATUT
 */
public class TrackView extends javax.swing.JPanel {

    final AudioPlayerView apv;
    final Track track;
    private final MixAudioBuffer mix;

    /** Creates new form NewJPanel */
    public TrackView() {
        apv = null;
        track = null;
        mix=null;
        initComponents();
    }

    public TrackView(AudioPlayerView apv,MixAudioBuffer mix, final Track track) {
        if (apv == null) {
            throw new IllegalArgumentException();
        }
        this.apv = apv;
        if (mix == null) {
            throw new IllegalArgumentException();
        }
        this.mix = mix;
        if (track == null) {
            throw new IllegalArgumentException();
        }
        this.track = track;
        initComponents();
        mute.setSelected(track.getMute());
        solo.setSelected(track.getSolo());
        volume.setValue((int) (track.getVolume() * volume.getMaximum()));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSeparator1 = new javax.swing.JSeparator();
        jSeparator3 = new javax.swing.JSeparator();
        jPanel3 = new javax.swing.JPanel();
        meterPanel = new javax.swing.JPanel();
        volume = new javax.swing.JSlider();
        mute = new javax.swing.JToggleButton();
        solo = new javax.swing.JToggleButton();
        jSeparator2 = new javax.swing.JSeparator();
        waveForm = new multitrack.preload.HeapAudioBufferView(apv,mix, track.buffer);

        setLayout(new java.awt.BorderLayout());

        jSeparator3.setForeground(new java.awt.Color(204, 204, 204));
        jSeparator3.setPreferredSize(new java.awt.Dimension(0, 1));
        add(jSeparator3, java.awt.BorderLayout.SOUTH);

        jPanel3.setLayout(new javax.swing.BoxLayout(jPanel3, javax.swing.BoxLayout.LINE_AXIS));

        meterPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 2, 1, 1));
        meterPanel.setLayout(new javax.swing.BoxLayout(meterPanel, javax.swing.BoxLayout.LINE_AXIS));

        volume.setMaximum(1024);
        volume.setValue(0);
        volume.setPreferredSize(new java.awt.Dimension(100, 25));
        volume.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                volumeStateChanged(evt);
            }
        });
        meterPanel.add(volume);

        mute.setText("M");
        mute.setMargin(new java.awt.Insets(1, 4, 0, 2));
        mute.setMaximumSize(new java.awt.Dimension(20, 20));
        mute.setMinimumSize(new java.awt.Dimension(20, 20));
        mute.setPreferredSize(new java.awt.Dimension(20, 20));
        mute.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                muteActionPerformed(evt);
            }
        });
        meterPanel.add(mute);

        solo.setText("S");
        solo.setMargin(new java.awt.Insets(1, 4, 0, 2));
        solo.setMaximumSize(new java.awt.Dimension(20, 20));
        solo.setMinimumSize(new java.awt.Dimension(20, 20));
        solo.setPreferredSize(new java.awt.Dimension(20, 20));
        solo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                soloActionPerformed(evt);
            }
        });
        meterPanel.add(solo);

        jPanel3.add(meterPanel);

        jSeparator2.setForeground(new java.awt.Color(204, 204, 204));
        jSeparator2.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jSeparator2.setPreferredSize(new java.awt.Dimension(1, 0));
        jPanel3.add(jSeparator2);

        add(jPanel3, java.awt.BorderLayout.WEST);

        javax.swing.GroupLayout waveFormLayout = new javax.swing.GroupLayout(waveForm);
        waveForm.setLayout(waveFormLayout);
        waveFormLayout.setHorizontalGroup(
            waveFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 447, Short.MAX_VALUE)
        );
        waveFormLayout.setVerticalGroup(
            waveFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 27, Short.MAX_VALUE)
        );

        add(waveForm, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void muteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_muteActionPerformed
        track.setMute(mute.isSelected());
    }//GEN-LAST:event_muteActionPerformed

    private void soloActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_soloActionPerformed
        track.setSolo(solo.isSelected());
    }//GEN-LAST:event_soloActionPerformed

    private void volumeStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_volumeStateChanged
        track.setVolume((float) volume.getValue() / volume.getMaximum());
    }//GEN-LAST:event_volumeStateChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JPanel meterPanel;
    private javax.swing.JToggleButton mute;
    private javax.swing.JToggleButton solo;
    private javax.swing.JSlider volume;
    private javax.swing.JPanel waveForm;
    // End of variables declaration//GEN-END:variables

    void updatePlayCursor(int positionInFrame) {
        ((HeapAudioBufferView) waveForm).updatePlayCursor(positionInFrame);
    }
}
